#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QDebug>
#include <QMessageBox>
#include <QTableWidgetItem>
#include <QIcon>
#include <QString>

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("荣控智能");
    this->setWindowFlags(Qt::FramelessWindowHint);
    config = CabinetConfig::config();
//    config->configInit();
    ctrlReport = new CtrlReport();
    ctrlReport->setServerAddress(config->getCtrlAddress());
    connect(ctrlReport, SIGNAL(reportRst(bool)), this, SLOT(ctrlRst(bool)));


    sev = new server();
    sev->moveToThread(&thServer);
    sev->setServerAddress(config->getApiAddress());
    connect(sev, SIGNAL(fetch_goods_rst(QString)), this, SLOT(recv_goods_fetch_rst(QString)));
    connect(this, SIGNAL(fetchGoodsReq(QString,QString,QString)), sev, SLOT(fetchGoods(QString,QString,QString)));
    connect(sev, SIGNAL(new_report(ctrlInfo*)), ctrlReport, SLOT(newReport(ctrlInfo*)));

    keyboad = new Keyboad();
    connect(keyboad, SIGNAL(clicked(QString)), this, SLOT(keyBoadClicked(QString)));

    sysTimer = new QTimer(this);
    connect(sysTimer,SIGNAL(timeout()),this, SLOT(sysTimeout()));
    sysTimer->start(1000);
    selCase = QPoint(0,0);
    curUser = NULL;
    initCabinets(config->getAllLayout());
#if QT_VERSION > 0x50000
    initLocker();
#else
    devManager = new DeviceManager(this);
    win_setting = new Setting();

    connect(ui->tempMain, SIGNAL(reqSetting()), this, SLOT(showSetting()));
    connect(win_setting, SIGNAL(reqCloseSetting()), this, SLOT(closeSetting()));
    connect(win_setting, SIGNAL(updateCabinetInfo(QString,QString)), ui->tempMain, SLOT(updateCabInfo(QString,QString)));

    connect(devManager, SIGNAL(cardReaderData(QString)), this, SLOT(recvCardReader(QString)));
    connect(devManager, SIGNAL(codeScanData(QString)), this, SLOT(recvScanData(QString)));
#endif

    listUser = config->getUsers();
    ui->lock_fresh->setIcon(QIcon(":/image/update.ico"));
    ui->scan_reader->grabKeyboard();
    ui->logout->hide();
    ui->search_text->hide();
    ui->scan_reader->setAttribute(Qt::WA_InputMethodEnabled, false);
    ui->search_text->setAttribute(Qt::WA_InputMethodEnabled, false);
    ui->user_card->setAttribute(Qt::WA_InputMethodEnabled, false);
    ui->server_address->setText(config->getApiAddress());
    ui->ctrl_address->setText(config->getCtrlAddress());
    ui->cabinet_id->setText(config->getCabinetId());
    ui->depart_name->setText(config->getDepartName());
    ui->depart_id->setText(config->getDepartId());
//    ui->test->hide();
    if(!listUser.isEmpty())
        ui->set->hide();

    thServer.start();
#if QT_VERSION < 0x50000
    ui->stack_full->setCurrentIndex(0);
    ui->stack_full->removeWidget(ui->stack_full->currentWidget());
    ui->stack_full->addWidget(win_setting);
    this->resize(1366, 768);
    if(listUser.isEmpty())
    {
        ui->stack_full->setCurrentIndex(1);
    }
#else
    ui->stack_full->setCurrentIndex(0);
#endif
}

void MainWidget::globalTouch()
{
    if(curUser != NULL)
    {
//        qDebug()<<"globalTouch";
        loginTime = QDateTime::currentDateTime().addSecs(180);
    }
}

MainWidget::~MainWidget()
{
    thServer.quit();
    thServer.wait();
    win_setting->deleteLater();
    delete keyboad;
    delete ui;
}

void MainWidget::recvCardReader(QString cardId)
{
    int idx = cardIndex(cardId, listUser);
    qDebug()<<idx;
    qDebug()<<"currentIndex"<<ui->stack_full->currentIndex();

    if(ui->stack_full->currentIndex() == 1)
    {
        win_setting->recvCardReader(cardId);
    }

    if(idx == -1)
    {
        return;
    }

    curUser = listUser.at(idx);
    config->setCurUser(curUser);
    ui->tempMain->updateLoginUser(curUser);

    loginTime = QDateTime::currentDateTime().addSecs(180);
}

void MainWidget::recvScanData(QString scanData)
{
    if(curUser == NULL)
        return;
    else
    {
        emit fetchGoodsReq(config->getCabinetId(), curUser->id, scanData.toUpper());
        ui->tempMain->updateScanData(scanData);
        updateTempMsg("正在提交");
    }

}

void MainWidget::showSetting()
{
    win_setting->updateUserTable();
    ui->stack_full->setCurrentWidget(win_setting);
}

void MainWidget::closeSetting()
{
    ui->stack_full->setCurrentIndex(0);
    if(!listUser.isEmpty())
        qDeleteAll(listUser.begin(), listUser.end());
    listUser = config->getUsers();
}

void MainWidget::selCaseChanged(QPoint pos)
{
    if(ui->stack_panel->currentIndex() == 0)
    {
        if(curUser == NULL)
            return;
        if((curUser->post == "护士") && pos == spc)
            return;
        locker->writeData(listCabinet[pos.x()]->getOpenData(pos.y()));
    }
    else if(ui->stack_panel->currentIndex() == 1)
    {
        ui->selCol->setText(QString::number(pos.x()));
        ui->selRow->setText(QString::number(pos.y()));
        if(!ui->set_scr->isChecked())
        {
            ui->scr_col->setValue(pos.x());
            ui->scr_row->setValue(pos.y());
        }
        selCase = pos;
        Cabinet* cab = listCabinet.at(selCase.x());
        ui->lock_seq->setValue(cab->getCtrlSeq(selCase.y()));
        ui->lock_io->setValue(cab->getCtrlIo(selCase.y()));
    }
}

void MainWidget::ctrlRst(bool success)
{
    if(success)
    {
        ui->api_msg->setText(optMsg + " 已提交到防控系统");
        updateTempMsg(optMsg + " 已提交到防控系统");
    }
    else
    {
        ui->api_msg->setText(optMsg + " 提交到防控系统失败");
        updateTempMsg(optMsg + " 提交到防控系统失败");
    }
    optMsg.clear();
}

void MainWidget::keyBoadClicked(QString str)
{
//    qDebug()<<"key"<<str;
    if(str == "关闭")
    {
        keyboad->hide();
    }
    else if(str == "清空")
    {
        ui->search_text->clear();
        clearSearch();
    }
    else if(str == "删除")
    {
        QString searchStr = ui->search_text->text();
        ui->search_text->setText(searchStr.left(searchStr.length()-1));
    }
    else
    {
        QString searchStr = ui->search_text->text();
        ui->search_text->setText(searchStr+str);
    }
}

void MainWidget::cabinetTextsChanged(int col, QStringList texts)
{
    config->setTexts(col, texts);
}

void MainWidget::sysTimeout()
{
#if QT_VERSION > 0x50000
    ui->time_shower->setText(QDateTime::currentDateTime().toString("yyyy年MM月dd日 hh:mm:ss"));
//    qDebug()<<QDateTime::currentDateTime().toString("yyyy年MM月dd日 hh:mm:ss")<<loginTime.toString("yyyy年MM月dd日 hh:mm:ss");
    if((curUser != NULL) && (QDateTime::currentDateTime()>loginTime))
    {
        on_logout_clicked();
    }
#else
    ui->tempMain->updateTime();
    if((curUser != NULL) && (QDateTime::currentDateTime()>loginTime))
    {
        ui->tempMain->logOut();
        curUser = NULL;
        config->setCurUser(NULL);
    }
#endif
}

void MainWidget::recv_goods_fetch_rst(QString msg)
{
    qDebug()<<"[recv_goods_fetch_rst]"<<msg;
    ui->api_msg->setText(msg);
    updateTempMsg(msg);
    optMsg = msg;
//    QMessageBox::information(this, "接口返回", msg, QMessageBox::Ok);
}

void MainWidget::on_set_clicked()
{
    ui->stack_menu->setCurrentIndex(1);
    ui->stack_panel->setCurrentIndex(1);
    ui->scan_reader->releaseKeyboard();
    foreach(Cabinet* cab, listCabinet)
    {
        ui->cabLayout_2->addWidget(cab);
        cab->setEditState(true);
    }
}

void MainWidget::on_mainPage_back_clicked()
{
    ui->stack_menu->setCurrentIndex(0);
    ui->stack_panel->setCurrentIndex(0);
    ui->scan_reader->grabKeyboard();
    foreach(Cabinet* cab, listCabinet)
    {
        cab->setEditState(false);
    }
}

void MainWidget::on_cab_add_clicked()
{
    QString cabLayout = ui->layout_sel->currentText();
    Cabinet* cab = new Cabinet(listCabinet.count(), cabLayout);
    connect(cab, SIGNAL(caseClicked(QPoint)), this, SLOT(selCaseChanged(QPoint)));
    listCabinet<<cab;
    ui->cabLayout_2->addWidget(cab);
    ui->scr_col->setMaximum(listCabinet.count()-1);
    config->setLayout(listCabinet.count()-1, cabLayout);
    config->setCabinetNum(listCabinet.count());
}

void MainWidget::on_cab_minus_clicked()
{
    Cabinet* cab = listCabinet.last();
    listCabinet.removeLast();
    ui->cabLayout_2->removeWidget(cab);
    config->removeCabinet(listCabinet.count());
    config->setCabinetNum(listCabinet.count());
    delete cab;
    if(listCabinet.isEmpty())
        ui->scr_col->setMaximum(0);
    else
        ui->scr_col->setMaximum(listCabinet.count()-1);
}


void MainWidget::on_scr_col_valueChanged(int arg1)
{
    if(arg1<0 || arg1>=listCabinet.count())
        return;
    ui->scr_row->setMaximum(listCabinet[arg1]->rowCount());
}

void MainWidget::on_set_scr_clicked(bool checked)
{
    if(checked)
    {
        QPoint pos(ui->scr_col->value(), ui->scr_row->value());
        listCabinet[pos.x()]->setScreenPos(pos.y());
        config->setScreenPos(pos);
    }
    else
    {
        QPoint pos(ui->scr_col->value(), ui->scr_row->value());
        listCabinet[pos.x()]->clearScreen();
        config->setScreenPos(QPoint(0,0));
    }
}

void MainWidget::on_set_scr_toggled(bool checked)
{
    if(checked)
    {
        ui->set_scr->setText("取消");
    }
    else
    {
        ui->set_scr->setText("确定");
    }
}

void MainWidget::on_set_lock_clicked()
{
    listCabinet[selCase.x()]->setCtrlSeq(selCase.y(), ui->lock_seq->value());
    listCabinet[selCase.x()]->setCtrlIo(selCase.y(), ui->lock_io->value());
    config->setCtrlSeq(selCase.x(), listCabinet[selCase.x()]->getCtrlSeq());
    config->setCtrlIo(selCase.x(), listCabinet[selCase.x()]->getCtrlIo());
}

void MainWidget::initCabinets(QStringList layouts)
{
    if(layouts.isEmpty())
        return;

    foreach(QString lay, layouts)
    {
        qDebug()<<lay;
        Cabinet* cab = new Cabinet(listCabinet.count(), lay);
        connect(cab, SIGNAL(caseClicked(QPoint)), this, SLOT(selCaseChanged(QPoint)));
        cab->setCtrlSeq(config->getCtrlSeq(listCabinet.count()));
        cab->setCtrlIo(config->getCtrlIo(listCabinet.count()));
        cab->setText(config->getTexts(listCabinet.count()));
        connect(cab, SIGNAL(caseTextsChanged(int,QStringList)), this, SLOT(cabinetTextsChanged(int,QStringList)));
        listCabinet<<cab;
        ui->cabLayout->addWidget(cab);
    }
    ui->scr_col->setMaximum(listCabinet.count()-1);
    QPoint scrPos = config->getScreenPos();
    listCabinet[scrPos.x()]->setScreenPos(scrPos.y());
    ui->scr_col->setValue(scrPos.x());
    ui->scr_row->setValue(scrPos.y());
    ui->set_scr->setChecked(true);

    ui->cabinetId->setText(config->getCabinetId());
    ui->departName->setText(config->getDepartName());
    spc = config->getSpecialCase();
    ui->spc_col->setText(QString::number(spc.x()));
    ui->spc_row->setText(QString::number(spc.y()));
}

void MainWidget::initLocker()
{
    QString devName = config->getLockDev();
    int baudrate = config->getLockBaudrate();
    locker = new LockCtrl(this);
    locker->initCom(devName, baudrate);
    updateComDev();
    ui->lock_dev->setEditText(devName);
    ui->lock_baudrate->setEditText(QString::number(baudrate));
}

int MainWidget::userIndex(QString id,QList<UserInfo *> users)
{
    int i = 0;
    foreach(UserInfo* info, users)
    {
        if(info->id == id)
        {
            config->setCurUser(info);
            return i;
        }
        i++;
    }
    return -1;
}

int MainWidget::cardIndex(QString card, QList<UserInfo *> users)
{
    int i = 0;
    foreach(UserInfo* info, users)
    {
        if(info->card == card)
            return i;
        i++;
    }
    return -1;
}

void MainWidget::updateCabLayout(QHBoxLayout *lay, QList<Cabinet *> cabs)
{
    foreach(Cabinet* cab, cabs)
    {
        lay->addWidget(cab);
    }
}

void MainWidget::selUser(int idx)
{
    if((idx < 0) || (idx > listUser.count()))
    {
        return;
    }
    UserInfo* user = listUser[idx];
    ui->user_name->setText(user->name);
    ui->user_id->setText(user->id);
    ui->user_card->setText(user->card);
    if(idx == 0)
    {
        if(ui->user_post->count() == 2)
            ui->user_post->addItem("管理员");
        ui->user_post->setEnabled(false);
        ui->user_name->setEnabled(true);
        ui->user_id->setEnabled(true);
        ui->user_card->setEnabled(true);
        ui->user_add->setEnabled(true);
        ui->user_del->setEnabled(false);
        ui->user_post->setEditText(user->post);
    }
    else
    {
        ui->user_post->setEnabled(true);
        ui->user_name->setEnabled(true);
        ui->user_id->setEnabled(true);
        ui->user_card->setEnabled(true);
        ui->user_add->setEnabled(true);
        ui->user_del->setEnabled(true);
        if(ui->user_post->count() == 3)
            ui->user_post->removeItem(2);
        if(user->post == "护士")
            ui->user_post->setCurrentIndex(0);
        else if(user->post == "护士长")
            ui->user_post->setCurrentIndex(1);
    }
}

void MainWidget::pushScan()
{
    qDebug()<<"[push]"<<ui->scan_reader->text();
    ui->api_msg->setText("正在提交");
    updateTempMsg("正在提交");
    emit fetchGoodsReq(config->getCabinetId(), curUser->id, ui->scan_reader->text().toUpper());
    ui->scan_reader->clear();
}

void MainWidget::checkUser()
{
    QString cardId = ui->scan_reader->text().toLower();
    ui->scan_reader->clear();
    int idx = cardIndex(cardId, listUser);
    qDebug()<<idx;
    if(idx == -1)
        return;
    if(listUser.at(idx)->post == "管理员")
        ui->set->show();
    curUser = listUser.at(idx);
    config->setCurUser(curUser);
    if(curUser->post == "护士长")
        locker->writeData(listCabinet.at(spc.x())->getOpenData(spc.y()));
    ui->logout->show();
    ui->hello->setText(QString("你好！%1").arg(curUser->name));
    loginTime = QDateTime::currentDateTime().addSecs(180);
}

void MainWidget::clearSearch()
{
    if(listCabinet.isEmpty())
        return;

    foreach(Cabinet* cab, listCabinet)
    {
        cab->searchClear();
    }
}

UserInfo* MainWidget::checkInfo()
{
    QString name = ui->user_name->text();
    QString card = ui->user_card->text().toLower();
    QString post = ui->user_post->currentText();
    QString id = ui->user_id->text();
    if(name.isEmpty() || card.isEmpty() || post.isEmpty() || id.isEmpty())
        return NULL;
//    if(userIndex(id, listUser) != -1)
//        return NULL;

    return new UserInfo(name, post, id, card);
}

void MainWidget::updateUserTable()
{
    int i=0;
    ui->users->clearContents();
    ui->users->setRowCount(listUser.count());
    ui->users->setColumnCount(4);

    foreach(UserInfo* info, listUser)
    {
        ui->users->setItem(i,0,new QTableWidgetItem(info->name));
        ui->users->setItem(i,1,new QTableWidgetItem(info->post));
        ui->users->setItem(i,2,new QTableWidgetItem(info->id));
        ui->users->setItem(i,3,new QTableWidgetItem(info->card));
        i++;
    }
}

void MainWidget::updateComDev()
{
    ui->lock_dev->clear();
#if QT_VERSION > 0x500000
    ui->lock_dev->addItems(locker->getDevList());
#endif
}

void MainWidget::updateTempMsg(QString msg)
{
#if QT_VERSION < 0x500000
    ui->tempMain->updateMsg(msg);
#endif
}

void MainWidget::on_lock_auto_clicked()
{
    if(selCase == QPoint(0,0))
    {
        ui->lock_seq->setValue(0);
        ui->lock_io->setValue(0);
    }
    else if(selCase.y() == 0)
    {
        Cabinet* cab = listCabinet.at(selCase.x()-1);
        ui->lock_seq->setValue(cab->getCtrlSeq(cab->rowCount()-1));
        ui->lock_io->setValue(cab->getCtrlIo(cab->rowCount()-1));
    }
    else
    {
        Cabinet* cab = listCabinet.at(selCase.x());
        ui->lock_seq->setValue(cab->getCtrlSeq(selCase.y()-1));
        ui->lock_io->setValue(cab->getCtrlIo(selCase.y()-1)+1);
    }
}

void MainWidget::on_menu_user_clicked()
{
    ui->stack_panel->setCurrentIndex(2);
    ui->cardId->hide();
    ui->cardId->grabKeyboard();
    if(listUser.isEmpty())
    {
        ui->check_msg->setText("当前无用户，请刷卡添加管理员");
    }
    else
    {
        ui->check_msg->setText("请刷卡验证管理员");
    }
}

void MainWidget::on_menu_layout_clicked()
{
    ui->cardId->releaseKeyboard();
    ui->stack_panel->setCurrentIndex(1);
    foreach(Cabinet* cab, listCabinet)
    {
        ui->cabLayout_2->addWidget(cab);
        cab->setEditState(true);
    }
}

void MainWidget::on_cardId_editingFinished()
{
    qDebug()<<ui->cardId->text();
    QString cardText = ui->cardId->text().toLower();
    int idx = cardIndex(cardText, listUser);
    ui->cardId->clear();

    if(listUser.isEmpty())
    {
        QStringList posts;
        posts<<"管理员";
        ui->user_post->clear();
        ui->user_post->addItems(posts);
        ui->user_card->setText(cardText);
        ui->stack_panel->setCurrentIndex(3);
    }
    else if(idx < 0)
    {
        return;
    }
    else if(listUser.at(idx)->post == "管理员")//管理员用户
    {
        QStringList posts;
        posts<<"护士"<<"护士长";
        ui->user_post->clear();
        ui->user_post->addItems(posts);
        ui->stack_panel->setCurrentIndex(3);
    }
    else
        return;

    ui->user_card->setFocus();
    updateUserTable();
    ui->cardId->releaseKeyboard();
//    updateComDev();
}

void MainWidget::on_user_add_clicked()
{
    UserInfo* info = checkInfo();
    if(info == NULL)
    {
        QMessageBox::information(this, "添加用户", "添加用户失败:用户信息不完整", QMessageBox::Ok);
        return;
    }
    int idx = userIndex(info->id, listUser);
    if(idx == -1)
    {
        if(cardIndex(info->card, listUser) != -1)
        {
            QMessageBox::information(this, "添加用户", "添加用户失败:卡号重复", QMessageBox::Ok);
            return;
        }
        if(!listUser.isEmpty())
        {
            if((info->post == "管理员") && (info->id != listUser.at(0)->id))
            {
                QMessageBox::information(this, "添加用户", "添加用户失败:不允许修改管理员ID", QMessageBox::Ok);
                return;
            }
        }
        listUser<<info;
        config->addUser(info);
        updateUserTable();
        if(info->post == "管理员")
        {
            QStringList posts;
            posts<<"护士"<<"护士长";
            ui->user_post->clear();
            ui->user_post->addItems(posts);
            ui->stack_panel->setCurrentIndex(3);
        }
    }
    else
    {
        if(idx == 0 && info->post != "管理员")
        {
            QMessageBox::information(this, "添加用户", "添加用户失败:将管理员变更为非管理员", QMessageBox::Ok);
            return;
        }
        if((cardIndex(info->card, listUser) != -1) && (cardIndex(info->card, listUser) != idx))
        {
            QMessageBox::information(this, "添加用户", "添加用户失败:卡号重复", QMessageBox::Ok);
            return;
        }
        if((info->post == "管理员") && (info->id != listUser.at(0)->id))
        {
            QMessageBox::information(this, "添加用户", "添加用户失败:不允许修改管理员id", QMessageBox::Ok);
            return;
        }
        listUser.removeAt(idx);
        listUser.insert(idx, info);
        config->addUser(info);
        updateUserTable();
    }
    QMessageBox::information(this, "添加用户", "添加用户成功", QMessageBox::Ok);
}

void MainWidget::on_user_del_clicked()
{
    UserInfo* info = checkInfo();
    if(info == NULL)
    {
        QMessageBox::information(this, "删除用户", "删除用户失败", QMessageBox::Ok);
        return;
    }
    int index = userIndex(info->id, listUser);
    if(index == -1)
    {
        QMessageBox::information(this, "删除用户", "删除用户失败：用户不存在", QMessageBox::Ok);
        return;
    }
    info = listUser.at(index);
    listUser.removeAt(index);
    config->removeUser(info);
    updateUserTable();
    QMessageBox::information(this, "删除用户", "删除用户成功", QMessageBox::Ok);
}

void MainWidget::on_stack_panel_currentChanged(int arg1)
{
    if(arg1 == 0)
    {
        updateCabLayout(ui->cabLayout, listCabinet);
    }
}

void MainWidget::on_users_cellClicked(int row, int)
{
//    emit selUserChanged(row);
    selUser(row);
}

void MainWidget::on_user_clear_clicked()
{
    ui->user_post->setEnabled(true);
    ui->user_name->setEnabled(true);
    ui->user_id->setEnabled(true);
    ui->user_card->setEnabled(true);
    ui->user_add->setEnabled(true);
    ui->user_del->setEnabled(true);

    ui->user_post->clear();
    ui->user_name->clear();
    ui->user_card->clear();
    ui->user_id->clear();

    QStringList posts;
    posts<<"护士"<<"护士长";
    ui->user_post->clear();
    ui->user_post->addItems(posts);
}

void MainWidget::on_lock_open_clicked()
{
    locker->openLock(ui->lock_seq->value(), ui->lock_io->value());
}

void MainWidget::on_lock_fresh_clicked()
{
    ui->lock_dev->clear();
#if QT_VERSION > 0x500000
    ui->lock_dev->addItems(locker->getDevList());
#endif
}

void MainWidget::on_lock_save_clicked()
{
    QString devName = ui->lock_dev->currentText();
    QString baudrate = ui->lock_baudrate->currentText();
    config->setLockDev(devName);
    config->setLockBaudrate(baudrate.toInt());
    locker->initCom(devName, baudrate.toInt());
}

void MainWidget::on_cabinet_id_editingFinished()
{
    QString cabId = ui->cabinet_id->text();
    config->setCabinetId(cabId);
    ui->cabinetId->setText(cabId);
}

void MainWidget::on_spc_save_clicked()
{
    config->setSpecialCase(ui->spc_col->text().toInt(), ui->spc_row->text().toInt());
}

void MainWidget::on_scan_reader_returnPressed()
{
    if(curUser != NULL)
        pushScan();
    else
        checkUser();
}

void MainWidget::on_scan_push_clicked()
{
    if(curUser != NULL)
        pushScan();
    else
        checkUser();
}

void MainWidget::on_logout_clicked()
{
    curUser = NULL;
    ui->logout->hide();
    ui->set->hide();
    ui->hello->clear();
    ui->api_msg->clear();
    config->setCurUser(NULL);
}


void MainWidget::on_search_toggled(bool checked)
{
    if(checked)
    {
        ui->search_text->show();
        QPoint keyboadPos = ui->frame_3->mapToGlobal(QPoint(ui->search->geometry().right()-keyboad->geometry().width()+10, ui->search->geometry().bottom()+8));
        keyboad->move(keyboadPos);
        keyboad->show();
//        ui->search_text->grabKeyboard();
    }
    else
    {
        ui->search_text->hide();
        keyboad->hide();
//        ui->scan_reader->grabKeyboard();
//        on_search_text_returnPressed();
    }
}

void MainWidget::on_search_text_textChanged(const QString &arg1)
{
    clearSearch();
    if(arg1.isEmpty())
    {
        return;
    }

    if(listCabinet.isEmpty())
        return;

    foreach(Cabinet* cab, listCabinet)
    {
        cab->findGoodsByPinyin(arg1);
    }
}

void MainWidget::on_search_text_returnPressed()
{
    ui->search_text->clear();
    if(listCabinet.isEmpty())
        return;

    foreach(Cabinet* cab, listCabinet)
    {
        cab->searchClear();
    }
}

void MainWidget::on_depart_name_editingFinished()
{
    QString departName = ui->depart_name->text();
    config->setDepartName(departName);
//    ui->departName->setText(departName);
}

void MainWidget::on_server_address_editingFinished()
{
    config->setApiAddress(ui->server_address->text());
    sev->setServerAddress(ui->server_address->text());
}

void MainWidget::on_ctrl_address_editingFinished()
{
    config->setCtrlAddress(ui->ctrl_address->text());
    ctrlReport->setServerAddress(ui->ctrl_address->text());
}

void MainWidget::on_depart_id_editingFinished()
{
    config->setDepartId(ui->depart_id->text());
}

//void MainWidget::on_test_clicked()
//{
//    sev->test();
//}

